//
//  UILabel+AutoSize.m
//  Courses
//
//  Created by xxx on 15/1/11.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "UILabel+AutoSize.h"
#import "Courses-Prefix.pch"
@implementation UILabel (AutoSize)

- (void)autoSetSizeWithPreferedWidth:(CGFloat)width preferedHeight:(CGFloat)height
{
    self.numberOfLines = 0;
    CGSize size = [self.text sizeWithFont:self.font
                        constrainedToSize:CGSizeMake(FLT_MAX, FLT_MAX)
                            lineBreakMode:NSLineBreakByCharWrapping];
    
    if (size.width < width) {
        width = size.width;
    } else {
        size = [self.text sizeWithFont:self.font
                     constrainedToSize:CGSizeMake(width, FLT_MAX)
                         lineBreakMode:NSLineBreakByCharWrapping];
        height = size.height;
        self.lineBreakMode = NSLineBreakByCharWrapping;
    }
    self.size = size;
}

- (void)autoSetSizeWithInWidth:(CGFloat)width preferedHeight:(CGFloat)height
{
    self.numberOfLines = 1;
    CGSize size = [self.text sizeWithFont:self.font
                        constrainedToSize:CGSizeMake(FLT_MAX, FLT_MAX)
                            lineBreakMode:NSLineBreakByCharWrapping];
    
    if (size.width < width) {
        width = size.width;
    } else {
        size.width = width;
    }
    self.size = size;
}

- (UIView *)autoSizedBackgroundViewWithRadius:(CGFloat)radius color:(UIColor *)color
{
    UIView *backgroundView = [[UIView alloc] initWithFrame:self.frame];
    [backgroundView.superview bringSubviewToFront:backgroundView];
    backgroundView.backgroundColor = color;
    backgroundView.hidden = YES;
    return backgroundView;
}

@end
